//
//  XLNetTest.m
//  Commet
//
//  Created by tanqilong on 16/6/27.
//  Copyright © 2016年 tanqilong. All rights reserved.
//

#import "XLNetTest.h"
#import "AFNetworking.h"

@implementation XLNetTest

+(void)requestWithDic:(NSDictionary *)paramDic ModeClass:(Class)modeClass Url:(NSString *)url Success:(void (^)(XLBaseNetMode *, id))successBlock Error:(void (^)(id))errorBlock {
   
}

+(void)requestWithDic:(NSDictionary *)paramDic ModeClass:(Class)modeClass Url:(NSString *)url Method:(RequestMothod)requestMethod Success:(void (^)(XLBaseNetMode *, id))successBlock Error:(void (^)(id json,NSURLResponse * response))errorBlock {
    NSURLSessionConfiguration * config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    AFURLSessionManager * manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:config];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSURLRequest * request;
    if (requestMethod == RequestMothodGet || requestMethod == 0) {
        request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:url parameters:paramDic error:nil];
    } else {
        request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:paramDic error:nil];
    }
    
    NSURLSessionTask * task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (!error) {
            [self successDeal:successBlock id:dic Class:modeClass];
        } else {
            errorBlock(responseObject,response);
            NSAssert(NO, @"网络请求:%@出错",request.URL);
        }
    }];
    [task resume];
}

#pragma mark --回调处理
+(void)successDeal:(void (^)(XLBaseNetMode *, id))successBlock id:(id)json Class:(Class)modeClass {
    XLBaseNetMode * mode = [[modeClass alloc]initWithDataDic:json];
    successBlock(mode,json);
}


@end
